package bbs.service;

import bbs.exception.ServiceException;
import bbs.mapper.TopicMapper;
import bbs.model.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ljg;
 * @create 2022-06-08 10:27
 */
@Service
@Transactional(rollbackFor = {RuntimeException.class, ServiceException.class})
public class TopicService {

    @Autowired
    TopicMapper topicMapper;

    @Autowired
    BoardService boardService ;

    public List<Topic> getTopicByBoardId(Integer boardId) {
        List<Topic> topicByBoardId = topicMapper.getTopicByBoardId(boardId);

        // 一对一的处理方案 可以使用 Java 代码进行实现
        // 也可以使用 Mybatis 中的一对一 来实现
//        topicByBoardId.forEach( topic -> {
//            topic.setUser( userService.findById( topic.getUserId() ) );
//            topic.setBoard( boardService.findById( topic.getBoardId() ) ) ;
//        });

        return topicByBoardId;
    }

    /**
     * @param topicId 主题id
     * @return  返回 topic对象
     */
    public Topic findById(Integer topicId) {
        return topicMapper.findById(topicId);
    }
}
